<div class="form">

<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'company-address-form',
	'enableAjaxValidation'=>false,
)); ?>

	<p class="note">Fields with <span class="required">*</span> are required.</p>
	
	<table class="group" id="address" cellpadding="0" cellspacing="0" border="0">
            <thead>
            	<tr>
            		<td><?php echo CHtml::label("Address", 'address'); ?></td>
            	</tr>
            </thead>
            <tbody>
			<tr>
				<td>
					<?php echo $form->TextField($model['address'], "type", array('placeholder'=>'Labeling the address')); ?>
					<?php echo $form->TextField($model['address'], "postal",array('maxlength'=>20,'placeholder'=>'Address postal code')); ?><br />
				</td>
                </tr>
                <tr>
				<td>
					<?php echo $form->TextArea($model['address'],"address",array('rows'=>3, 'cols'=>42)); ?>
				</td>
                </tr>
                <tr>
				<td>
					<?php echo $form->textField($model['address'],'operation_time',array('maxlength'=>50,'placeholder'=>'Operation timing')); ?>
					<?php echo $form->error($model['address'],'operation_time'); ?>
					<?php echo $form->textField($model['address'],'avoid_time',array('maxlength'=>50,'placeholder'=>'Avoid timing')); ?>
					<?php echo $form->error($model['address'],'avoid_time'); ?>
				</td>
                </tr>
            </tbody>
    </table>	
	<div class="row">	
            <table class="group" id="contact" cellpadding="0" cellspacing="0" border="0">
            	<thead>
            		<tr>
            			<td><?php echo CHtml::label("Tel No. - ".CHtml::link('Add','#',array('id'=>'add')), 'telephone'); ?></td>
            		</tr>
            	</thead>
            	<tbody>
			<?php foreach($model['contact'] as $i=>$item): ?>
				<tr>
					<td>
						<?php echo $form->DropDownList($item, "[$i]type", $item->teleType,array('style'=>'width:9em;')); ?>
						<?php echo $form->TextField($item,"[$i]ref",array('size'=>25,'maxlength'=>35,'numberOnly'=>1)); ?>
					</td>
                </tr>
            <?php endforeach; ?>
            	</tbody>
            </table>
	</div>
	<div class="row">
		<table class="group" id="email" cellpadding="0" cellspacing="0" border="0">
           	<thead>
           		<tr>
           			<td><?php echo CHtml::label("Email - ".CHtml::link('Add','#',array('id'=>'add')), 'email'); ?> </td>
           		</tr>
           	</thead>
           	<tbody>
			<?php foreach($model['email'] as $i=>$item): ?>
				<tr>
					<td>
						<?php echo $form->DropDownList($item, "[$i]type", $item->emailType,array('style'=>'width:9em;')); ?>
						<?php echo $form->TextField($item,"[$i]ref",array('size'=>25,'maxlength'=>254)); ?>
					</td>
                </tr>
            <?php endforeach; ?>
           	</tbody>
        </table>
	</div>

	<div class="row buttons">
		<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
	</div>

<?php $this->endWidget(); ?>

</div><!-- form -->
<?php
$cs = Yii::app()->getClientScript();
$showUrl = CHtml::normalizeUrl(array('company/view'));
$script =<<<EOD
$.company_address_form={
	afterSubmit:function(data){
		if(data.id != '')
			window.location = '$showUrl?id='+data.id;
	}
}
var contactselect = $('div#contacts>div.row:last>select').clone();
var emailselect = $('div#email>div.row:last>select').clone();
$('a#add').click(function(){
	var table = $(this).parents('table');
	AddNewTableRow(table);
	return false;
});
$('div#contacts input[numberOnly=1]').live('keypress',function(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46) {
    	return false
    }
});
$('select').live('change',function(){
	if($(this).val() == "Others"){
		var html = "<input size='14' name='"+$(this).attr('name')+"' id='"+$(this).attr('id')+"' placeholder='Please specify' />";
		$(this).replaceWith(html);
	}
});
$('#company-address-form input[type=submit]').click(function(){
	var companyForm = $.extend($.simplelogform,$.company_address_form);
	companyForm.Submit($('#company-address-form'));	
	return false;
});
EOD;
$cs->registerScript('company_address#create',$script);
?>